﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data.Linq;

using System.Web.UI;
using System.Web.UI.WebControls;
using DongDong.Common;
using System.Data;

/// <summary>
///KCKBXX 的摘要说明
/// </summary>
public partial class KCKBXX
{
    public enum typeKCKBXX { 个人 = 1, 企业 };
    public enum dateKKRQ { 随到随学 = 1, 随报随学, 额满开课, 轮流开课, 滚动开课 };
    public enum dateSKRQ { 随到随学 = 1, 随报随学, 额满开课, 轮流开课, 滚动开课 };
    public enum kindSK { 走读 = 1, 网络 };

    public static void BadDelete(int ID)
    {
        using (DataClassesDataContext context = new DataClassesDataContext())
        {
            KCKBXX objKCKBXX = KCKBXX.GetByID(ID);
            if (objKCKBXX == null)
                return;
            objKCKBXX.Enable_Flag = 0;
            objKCKBXX.Update();
        }
    }

    public static KCKBXX GetByID(int id)
    {

        using (DataClassesDataContext context = new DataClassesDataContext())
        {
            return context.KCKBXX.FirstOrDefault(row => row.ID == id);
        }
    }

    public static List<KCKBXX> GetAll()
    {
        using (DataClassesDataContext context = new DataClassesDataContext())
        {
            return context.KCKBXX.ToList();
        }
    }

    public void Add()
    {
        using (DataClassesDataContext context = new DataClassesDataContext())
        {
            context.KCKBXX.InsertOnSubmit(this);
            context.SubmitChanges();
        }
    }

    public void Update()
    {
        using (DataClassesDataContext context = new DataClassesDataContext())
        {
            this.GetType().GetMethod("Detach").Invoke(this, null);
            var tbl = context.GetTable<KCKBXX>();
            tbl.Attach(this);
            context.Refresh(RefreshMode.KeepCurrentValues, this);
            context.SubmitChanges();
        }
    }

    public static void Delete(int id)
    {
        using (DataClassesDataContext context = new DataClassesDataContext())
        {
            var objItem = context.KCKBXX.FirstOrDefault(row => row.ID == id);
            if (objItem == null)
                return;
            context.KCKBXX.DeleteOnSubmit(objItem);
            context.SubmitChanges();
        }
    }

    public static List<KCKBJBXX> GetAllByCondition(int shengID, int shiID, int quID, int type, int KKRQ, int SKRQ, int SKFS, string KeHao, string AddTimeFrom, string AddTimeTo, string EndTimeFrom, string EndTimeTo, string KCZFY)
    {
        using (DataClassesDataContext context = new DataClassesDataContext())
        {
            var query = from KB in context.KCKBXX
                        join KC in context.KCJBXX
                        on KB.KCID equals KC.ID
                        where KB.Enable_Flag == 1
                        select new KCKBJBXX
                        {
                            ID = KB.ID,
                            KeHao = KB.KeHao,
                            KKRQ = KB.KKRQ,
                            SKRQ = KB.SKRQ,
                            SKFS = KB.SKFS,
                            ProvinceID = KB.ProvinceID,
                            CityID = KB.CityID,
                            ReginIDs = KB.ReginIDs,
                            Address = KB.Address,
                            KS_KC = KB.KS_KC,
                            TeacherID = KB.TeacherID,
                            ZSRS = KB.ZSRS,
                            AddTime = KB.AddTime,
                            EndTime = KB.EndTime,
                            KCZFY = KB.KCZFY,
                            FYMX = KB.FYMX,
                            JieDuanPrice = KB.JieDuanPrice,
                            JieDuanMS = KB.JieDuanMS,
                            JCPSFSID = KB.JCPSFSID,
                            CDXZ = KB.CDXZ,
                            TKHBXZ = KB.TKHBXZ,
                            QTSM = KB.QTSM,
                            KCID = KB.KCID,
                            JGID = KB.JGID,
                            Enable_Flag = KB.Enable_Flag,
                            KCMC = KC.KCMC,
                            TYPE = KC.Type
                        };
            if (shengID != 0)
            {
                query = query.Where(row => row.ProvinceID == shengID);
            }
            if (shiID != 0)
            {
                query = query.Where(row => row.CityID == shiID);
            }
            if (quID != 0)
            {
                query = query.Where(row => row.ReginIDs.Contains(quID.ToString()));            
            }
            if (type != 999)
            {
                query = query.Where(row => row.TYPE == type);
            }
            if (KKRQ == 9999)
            {
                query = query.Where(row => row.KKRQ != "1" && row.KKRQ != "2" && row.KKRQ != "3" && row.KKRQ != "4" && row.KKRQ != "5");
            }
            else if (KKRQ != 999)
            {
                query = query.Where(row => row.KKRQ == KKRQ.ToString());
            }
            if (SKRQ == 9999)
            {
                query = query.Where(row => row.SKRQ != "1" && row.SKRQ != "2" && row.SKRQ != "3" && row.SKRQ != "4" && row.SKRQ != "5");
            }
            else if (SKRQ != 999)
            {
                query = query.Where(row => row.SKRQ == SKRQ.ToString());
            }
            if (SKFS != 999)
            {
                query = query.Where(row => row.SKFS == SKFS);
            }
            if (!string.IsNullOrEmpty(KeHao))
            {
                query = query.Where(row => row.KeHao.Contains(KeHao));
            }
            if (!string.IsNullOrEmpty(AddTimeFrom))
            {
                query = query.Where(row => row.AddTime >= Misc.ConvertToDateTime(AddTimeFrom));
            }
            if (!string.IsNullOrEmpty(AddTimeTo))
            {
                query = query.Where(row => row.AddTime <= Misc.ConvertToDateTime(AddTimeTo));
            }
            if (!string.IsNullOrEmpty(EndTimeFrom))
            {
                query = query.Where(row => row.EndTime >= Misc.ConvertToDateTime(EndTimeFrom));
            }
            if (!string.IsNullOrEmpty(EndTimeTo))
            {
                query = query.Where(row => row.EndTime <= Misc.ConvertToDateTime(EndTimeTo));
            }
            if (!string.IsNullOrEmpty(KCZFY))
            {
                query = query.Where(row => row.KCZFY == Misc.ConvertToDecimal(KCZFY));
            }
            return query.ToList<KCKBJBXX>();
        }
    }

    public class KCKBJBXX
    {
        public int ID { get; set; }

        public string KeHao { get; set; }

        public string KKRQ { get; set; }

        public string SKRQ { get; set; }

        public int SKFS { get; set; }

        public int ProvinceID { get; set; }

        public int CityID { get; set; }

        public string ReginIDs { get; set; }

        public string Address { get; set; }

        public string KS_KC { get; set; }

        public string TeacherID { get; set; }

        public int ZSRS { get; set; }

        public System.DateTime AddTime { get; set; }

        public System.DateTime EndTime { get; set; }

        public decimal KCZFY { get; set; }

        public string FYMX { get; set; }

        public string JieDuanPrice { get; set; }

        public string JieDuanMS { get; set; }

        public int JCPSFSID { get; set; }

        public string CDXZ { get; set; }

        public string TKHBXZ { get; set; }

        public string QTSM { get; set; }

        public int KCID { get; set; }

        public int JGID { get; set; }

        public int Enable_Flag { get; set; }

        public string KCMC { get; set; }

        public int TYPE { get; set; }
    }
}